package bjtu.cit.bookshop.controller;

import bjtu.cit.bookshop.common.ErrorCode;
import bjtu.cit.bookshop.common.Md5;
import bjtu.cit.bookshop.entities.User;
import bjtu.cit.bookshop.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by jia19 on 2015/9/23.
 */
@Controller
@RequestMapping("/")
public class RegController extends BaseController {

    @Resource
    IUserService userService = null;

    @RequestMapping("reg.do")
    //打开注册页面
    public String reg(ModelMap model, HttpSession session) {
        String isLogin = (String)session.getAttribute("isLogin");
        if (isLogin == null || isLogin.equals("false"))
        {
            model.addAttribute("isLogin", false);
            return "reg";
        }
        else
        {
            model.addAttribute("isLogin", true);

            String loginName = (String)session.getAttribute("loginName");
            if (loginName != null){
                model.addAttribute("loginName", loginName);
            }
            return "alreadyLogin";
        }
    }

    @ResponseBody
    @RequestMapping("usernameValidate.do")
    public ErrorCode usernameValidate(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        User user = userService.getUserByUserName(username);
        if (user != null) {
            return USER_EXIST;
        }
        else {
            return SUCCESS;
        }
    }

    @ResponseBody
    @RequestMapping("donereg.do")
    //注册功能
    public ErrorCode doneReg(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String postcode = request.getParameter("postcode");

        //判断填写是否完整
        if (username.length() == 0 ||
                password.length() == 0 ||
                gender.length() == 0 ||
                address.length() == 0 ||
                email.length() == 0 ||
                phone.length() == 0 ||
                postcode.length() == 0) {
            return INFORMATION_MISSING;
        }

        //判断用户是否存在
        User user = userService.getUserByUserName(username);
        if (user == null) {
            user = new User();
            user.setAddress(address);
            user.setUsername(username);
            user.setEmail(email);
            user.setGender(gender);
            user.setPhone(phone);
            user.setPostcode(postcode);
            user.setPassword(Md5.getMd5(password));
            userService.createAUser(user);
            return SUCCESS;
        }
        else {
            return USER_EXIST;
        }


    }

}
